// pages/getgift/getgift.js

const host = require('../../config.js').host;
const network = require('../../utils/network.js');
const QR = require("../../utils/qrcode.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    mycanvas:'',
    nums:0,
    host:host,
    nodes:"欢迎光临本店，本店新开张，诚信经营，本店商品均属正品，假一罚十信誉保证。欢迎广大顾客前来放心选购件我将竭诚为您服务！本店的服务宗旨是用心服务，以诚待人！",
    alertErweima:false,
    giftQRCode:"",
    customerPrizeId:"", 
    detailDic:{},
    placeholder: '', //默认二维码生成文本

  },
  popErweima(){ 
    if(this.data.detailDic.prizeUseState==0){

// 页面初始化 options为页面跳转所带来的参数
var size = this.setCanvasSize(); //动态设置画布大小
var initUrl = this.data.placeholder;
this.setData({
  nums:this.data.nums+1,
  mycanvas:'mycanvas_'+this.data.nums
})
this.createQrCode(initUrl, this.data.mycanvas, size.w, size.h);


      this.setData({alertErweima:true});
    } 
  },
  touchStart(){},
  touchMove(){},
  touchEnd(){},
  hideErweima(){ 
    this.setData({alertErweima:false});
    this.getDetail();
  },
  getDetail() {
    let that = this;
    network.requestGet(host + '/xcx/customer-prize/prize-info', {
      "customerPrizeId": this.data.customerPrizeId,
    }, res => {  
      if (res.data.code == 200) {
        that.setData({ 
          detailDic:res.data.data
        })
        if(res.data.data.prizeUseState != 0){
          wx.navigateBack({
            delta: 0,
          })
        }
      } else {
        wx.showToast({
          title: res.data.msg ? res.data.msg : '请求失败',
          icon: "none",
          duration: 1200
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      customerPrizeId:options.customerPrizeId,
      placeholder:options.customerPrizeId
    })
    this.getDetail()
    
  },
   
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


  //适配不同屏幕大小的canvas
  setCanvasSize: function () {
    var size = {};
    try {
      var res = wx.getSystemInfoSync();
      var scale = 750 / 500; //不同屏幕下canvas的适配比例；设计稿是750宽
      var width = res.windowWidth / scale;
      var height = width; //canvas画布为正方形
      size.w = width;
      size.h = height;
    } catch (e) {
      // Do something when catch error
      console.log("获取设备信息失败" + e);
    }
    return size;
  },
  createQrCode: function (url, canvasId, cavW, cavH) {
    //调用插件中的draw方法，绘制二维码图片
    QR.api.draw(url, canvasId, cavW, cavH, this, this.canvasToTempImage);
    // setTimeout(() => { this.canvasToTempImage();},100);

  },
  //获取临时缓存照片路径，存入data中
  canvasToTempImage: function () {
    var that = this;
    wx.canvasToTempFilePath({
      canvasId: this.data.mycanvas,
      success: function (res) {
        var tempFilePath = res.tempFilePath;
        that.setData({
          imagePath: tempFilePath,
        });
      },
      fail: function (res) {
        console.log(res);
      }
    }, that);
  },
  //点击图片进行预览，长按保存分享图片
  previewImg: function (e) {
    var img = this.data.imagePath;
    wx.previewImage({
      current: img, // 当前显示图片的http链接
      urls: [img] // 需要预览的图片http链接列表
    })
  },
   

})